package loveappservice.controller;


import loveappservice.domain.Images;
import loveappservice.domain.Img;
import loveappservice.domain.Img_group;
import loveappservice.domain.Users;
import loveappservice.service.User_service;
import loveappservice.service.impl.User_service_impl;
import loveappservice.util.QiniuUtil;
import loveappservice.util.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class imagesController {
    //实例化Service类
    User_service user_service = new User_service_impl();
    /*实例化七牛云工具类*/
    QiniuUtil qiniuUtil = new QiniuUtil();

    /*查询所有相册*/
    @RequestMapping("/selectImages")
    public Result selectImages(){
        List<Images> images = user_service.selectImages();

        return Result.success(images);
    }

    /*查询某个相册的所有分组照片*/
    @RequestMapping("/selectImg_group")
    public Result selectImg_group(int images_id){
        System.out.println("传过来的相册ID"+images_id);
        List<Img_group> img_groups = user_service.selectImg_group(images_id);
        /*遍历分组,查询这些分组都有哪些照片,和分组所属的用户*/
        for (int i=0;i<img_groups.size();i++){
            /*获取相册照片*/
            List<Img> imgs = user_service.selcetImg(img_groups.get(i).getImg_group_id());
            /*获取相册用户信息*/
            Users users =user_service.finOneUser(img_groups.get(i).getUser_id());
            /*把相册和用户信息写进照片分组对象*/
            img_groups.get(i).setImg(imgs);
            img_groups.get(i).setUsers(users);

        }

        return Result.success(img_groups);
    }

    /*新增相册*/
    @RequestMapping("/addImages")
    public Result addImages(String images_name){
        if (images_name==null)
        {
            return Result.success("0");
        }else {
            int res = user_service.addImages(images_name);
            return Result.success(res);

        }
    }

    /*删除一个相册*/
    @RequestMapping("/deleteImages")
    public Result deleteImages(int images_id){
        /*调用服务删除一条纪念日*/
        boolean result = user_service.deleteImages(images_id);
        System.out.println(result);
        return Result.success(result);
    }

    /*新增一个分组*/
    @RequestMapping("/addGroup")
    public Result addGroup(int images_id,String img_group_desc ,int user_id){
        System.out.println(images_id+" "+img_group_desc+" "+user_id);

        /*获取当前时间*/
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /*把获取到的值赋给泛型*/
        Img_group img_group = new Img_group();
        img_group.setImages_id(images_id);
        img_group.setImg_group_desc(img_group_desc);
        img_group.setImg_group_time(formatter.format(date));
        img_group.setUser_id(user_id);

        return Result.success(user_service.addImgGroup(img_group));
    }


    /*接收上传的图片*/
    @RequestMapping("/uploadImages")
    public Result uploadImages(int images_id,int img_group_id ,String fileName, MultipartFile imgs) throws IOException {
        System.out.println(fileName);
        String img_url="http://xiaoziyan.top/love-app/images/";
        /*保存到本地*/
/*      String originalFilename = imgs.getOriginalFilename();
        System.out.println(originalFilename);git pull
        imgs.transferTo(new File("E:\\IDEAProject\\LoveAppService\\images\\"+fileName));*/

        /*查询相册名称*/
        String images_name = user_service.selectImages_name(images_id);
        System.out.println(images_name);
        /*保存到七牛云*/
        qiniuUtil.uploadImg(images_name,fileName,imgs);
        /*更新img库*/
        img_url = img_url+images_name+"/"+fileName;

        int res = user_service.addImg(images_id,img_group_id,img_url);
        /*更新相册照片总数*/
        user_service.upImg_total(images_id);

        return Result.success(res);
    }

    /*删除一个照片分组*/
    @RequestMapping("/deleteImg_group")
    public Result deleteImg_group(int img_group_id,int images_id){
        System.out.println(img_group_id);
        /*删除七牛云上面的照片*/
        if (user_service.deleteQiniuImg(img_group_id)){
            /*删除该分组下面的所有照片*/
            boolean res = user_service.deleteimg_Group(img_group_id);
            /*更新相册照片总数*/
            user_service.upImg_total(images_id);

            /*更新相册封面*/
            user_service.upImagesCoverUrl(images_id);
            return Result.success(res);
        }else return Result.error("false");

    }

    /*删除照片*/
    @RequestMapping("/deleteImg")
    public Result deleteImg(int img_id){


        return Result.success();
    }



}
